<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="sheltertypecode">
        <el-input
          v-model="queryParams.sheltertypecode"
          placeholder="请输入类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="级别" prop="level">
        <el-select v-model="queryParams.level" placeholder="请选择级别" clearable>
          <el-option
            v-for="dict in dict.type.type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="保密级别" prop="secretLevel">
        <el-select v-model="queryParams.secretLevel" placeholder="请选择保密级别" clearable>
          <el-option
            v-for="dict in dict.type.secret_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="使用时间" prop="useTime">
        <el-date-picker clearable
          v-model="queryParams.useTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择使用时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="使用年限" prop="useYearNum">
        <el-input
          v-model="queryParams.useYearNum"
          placeholder="请输入使用年限"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人" prop="respPerson">
        <el-input
          v-model="queryParams.respPerson"
          placeholder="请输入负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="主管单位" prop="chargeDept">
        <el-input
          v-model="queryParams.chargeDept"
          placeholder="请输入主管单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="面积" prop="area">
        <el-input
          v-model="queryParams.area"
          placeholder="请输入面积"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="容纳人数" prop="personNum">
        <el-input
          v-model="queryParams.personNum"
          placeholder="请输入容纳人数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['rm:shelter:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['rm:shelter:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['rm:shelter:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['rm:shelter:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shelterList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="类型" align="center" prop="sheltertypecode" />
      <el-table-column label="级别" align="center" prop="level">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.type" :value="scope.row.level"/>
        </template>
      </el-table-column>
      <el-table-column label="保密级别" align="center" prop="secretLevel">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.secret_type" :value="scope.row.secretLevel"/>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="联系电话" align="center" prop="telephone" />
      <el-table-column label="经度" align="center" prop="longitude" />
      <el-table-column label="纬度" align="center" prop="latitude" />
      <el-table-column label="使用时间" align="center" prop="useTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.useTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="使用年限" align="center" prop="useYearNum" />
      <el-table-column label="基本情况" align="center" prop="description" />
      <el-table-column label="负责人" align="center" prop="respPerson" />
      <el-table-column label="办公电话" align="center" prop="officeTel" />
      <el-table-column label="移动电话" align="center" prop="mobileTel" />
      <el-table-column label="电子邮箱" align="center" prop="email" />
      <el-table-column label="主管单位" align="center" prop="chargeDept" />
      <el-table-column label="面积" align="center" prop="area" />
      <el-table-column label="容纳人数" align="center" prop="personNum" />
      <el-table-column label="备注" align="center" prop="notes" />
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['rm:shelter:edit']"
            plain
          >修改</el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            plain
            v-hasPermi="['rm:shelter:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改应急避难所对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="sheltertypecode">
          <el-input v-model="form.sheltertypecode" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-select v-model="form.level" placeholder="请选择级别">
            <el-option
              v-for="dict in dict.type.type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="保密级别" prop="secretLevel">
          <el-select v-model="form.secretLevel" placeholder="请选择保密级别">
            <el-option
              v-for="dict in dict.type.secret_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="联系电话" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="使用时间" prop="useTime">
          <el-date-picker clearable
            v-model="form.useTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择使用时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用年限" prop="useYearNum">
          <el-input v-model="form.useYearNum" placeholder="请输入使用年限" />
        </el-form-item>
        <el-form-item label="基本情况" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="负责人" prop="respPerson">
          <el-input v-model="form.respPerson" placeholder="请输入负责人" />
        </el-form-item>
        <el-form-item label="办公电话" prop="officeTel">
          <el-input v-model="form.officeTel" placeholder="请输入办公电话" />
        </el-form-item>
        <el-form-item label="移动电话" prop="mobileTel">
          <el-input v-model="form.mobileTel" placeholder="请输入移动电话" />
        </el-form-item>
        <el-form-item label="电子邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入电子邮箱" />
        </el-form-item>
        <el-form-item label="主管单位" prop="chargeDept">
          <el-input v-model="form.chargeDept" placeholder="请输入主管单位" />
        </el-form-item>
        <el-form-item label="面积" prop="area">
          <el-input v-model="form.area" placeholder="请输入面积" />
        </el-form-item>
        <el-form-item label="容纳人数" prop="personNum">
          <el-input v-model="form.personNum" placeholder="请输入容纳人数" />
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShelter, getShelter, delShelter, addShelter, updateShelter } from "@/api/resourcemanagement/shelter";

export default {
  name: "Shelter",
  dicts: ['secret_type', 'type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应急避难所表格数据
      shelterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        sheltertypecode: null,
        level: null,
        secretLevel: null,
        address: null,
        useTime: null,
        useYearNum: null,
        respPerson: null,
        chargeDept: null,
        area: null,
        personNum: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        sheltertypecode: [
          { required: true, message: "类型不能为空", trigger: "blur" }
        ],
        level: [
          { required: true, message: "级别不能为空", trigger: "change" }
        ],
        secretLevel: [
          { required: true, message: "保密级别不能为空", trigger: "change" }
        ],
        telephone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        useTime: [
          { required: true, message: "使用时间不能为空", trigger: "blur" }
        ],
        useYearNum: [
          { required: true, message: "使用年限不能为空", trigger: "blur" }
        ],
        respPerson: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        mobileTel: [
          { required: true, message: "移动电话不能为空", trigger: "blur" }
        ],
        personNum: [
          { required: true, message: "容纳人数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询应急避难所列表 */
    getList() {
      this.loading = true;
      listShelter(this.queryParams).then(response => {
        this.shelterList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        sheltertypecode: null,
        level: null,
        secretLevel: null,
        address: null,
        telephone: null,
        longitude: null,
        latitude: null,
        useTime: null,
        useYearNum: null,
        description: null,
        respPerson: null,
        officeTel: null,
        mobileTel: null,
        email: null,
        chargeDept: null,
        area: null,
        personNum: null,
        createTime: null,
        updateTime: null,
        notes: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加应急避难所";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getShelter(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应急避难所";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateShelter(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShelter(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除应急避难所编号为"' + ids + '"的数据项？').then(function() {
        return delShelter(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('rm/shelter/export', {
        ...this.queryParams
      }, `shelter_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
